<% layout('layout/appLayout') %>
<% block('header').append(`<style>


.page__ft {
  position:fixed;
  width:100%;
  bottom:0;
  left:0;
  z-index: 100;
  background:#FFF;
  height:49px;
  line-height:49px;
  border-top:1px solid #e3e3e3;
}
.goods_section {
  font-size:14px;
  line-height:1.4;
  overflow-x:hidden;
}
.goods_section img {
  max-width:100%;
}
.goods_btn_primary {
  color:#FFF;
  display:inline-block;
  background:#58953b;
  padding:0 20px;
  text-align:center;
  border-left:1px solid #e3e3e3;
  border-right:1px solid #e3e3e3;
}
.goods_btn_warning {
  display:inline-block;
  padding:0 20px;
  text-align:center;
    color: #fff;
  background-color: #f0ad4e;
  border-color: #eea236;

}
.disabled {
  opacity:0.85;
}
.goods_btn_default {
  width:120px;
  color:#656565;
  display:inline-block;
  background:#FFF;
  padding:0 20px;
  text-align:center;
  border-left:1px solid #e3e3e3;
  border-right:1px solid #e3e3e3;

}
</style>`)%>
<div class="page has_head has_tab">
  <div class="page__hd weui-flex">
    <div style="width:60px;padding-left:10px;">
      <a class="back_btn" href="javascript:;" onclick="history.back()"><i class="iconfont icon-back"></i>返回</a>
    </div>
    <div class="weui-flex__item">
      <h1 class="title">商品详情</h1>
    </div>
    <div style="width:60px;"></div>
  </div>
  <div class="page__bd">
    <div class="light-bg">
      <div class="swiper-container" data-space-between='10' data-pagination='.swiper-pagination' data-autoplay="1000">
        <div class="swiper-wrapper">
          <% goods.imgs.forEach(function(item ){ %>
            <div class="swiper-slide"><img src="<%=item.replace(/_\$sma[^.]+/,'') %>" alt=""></div>
          <% })%>
        </div>
      </div>
      <div class="padding15-h padding-top padding-bottom border-bottom">
        <h1 class="product_name fs18"><%=goods.name %></h1>
        <p class="fs14 text-muted"><%=goods.subName%></p>
        <p class="fs14">
         <span class="fs20 weui-cell_warn">￥<%=goods.sellPrice%></span>
          <del class="padding-left20 text-muted" style="color:#989898;">原价:￥<%=goods.virPrice %> </del>
        </p>
        <!--<div class="product_stock margin-top">-->
          <!--<p class="fs14">库存：<%=goods.stock%></p>-->
        <!--</div>-->
      </div>
    </div>
    <div class="margin-top padding light-bg  border-top border-bottom">
      <p class="fs14 text-muted">库存：<%=goods.stock || ''%></p>
      <p class="fs14 text-muted">产地：<%=goods.place || ''%></p>
      <p class="fs14 text-muted">单位：<%=goods.unit || ''%></p>
    </div>
    <div class="margin-top light-bg padding border-top border-bottom">
      <h4 class="fs16 normal padding-bottom">商品详情</h4>
      <div class="goods_section">
        <%- goods.content %>
      </div>
    </div>
  </div>
  <div class="page__ft weui-flex">
    <div class="weui-flex__item padding-left15">
      <a class="" href="/app/cart" style="position:relative;width: 60px;display: block;">
        <span  id="cartBadge"  class="weui-badge <%=cartNum===0?' hide':'' %>" style="position: absolute;top: 0px;right: 2em;"><%=cartNum %></span>
        <i class="iconfont icon-cart fs22"></i></a>
    </div>
    <% if (goods.stock && goods.stock > 0) { %>
      <div style="margin-right:-2px;">
        <a id="quickBuy" class="goods_btn goods_btn_default">立即购买</a>
      </div>
      <div >
        <a id="addCart" class="goods_btn goods_btn_primary">加入购物车</a>
      </div>
    <% } else { %>
      <a href="javascript:void(0)" class="goods_btn goods_btn_warning disabled">已售罄</a>
    <% } %>
  </div>
</div>
<div id="numPopup" class="weui-popup__container popup-bottom " style="z-index:110;">
  <div class="weui-popup__overlay" ></div>
  <div class="weui-popup__modal">
    <div class="toolbar">
      <div class="toolbar-inner">
        <a href="javascript:;" class="picker-button close-popup">关闭</a>
        <h1 class="title">选择数量</h1>
      </div>
    </div>
    <div class="modal-content" >
      <form id="form" action="/app/pay/orderPay" method="post"  novalidate>
        <div class="weui-cells">
          <div class="weui-cell">
            <div class="weui-cell__bd">
              <p>选择数量</p>
            </div>
            <div class="weui-cell__ft padding-top5">
              <div class="spinner "  >
                <input name="num"  type="text" value="1" >
              </div>
              <input name="id" type="hidden" value="<%=goods._id%>">
              <input name="status" type="hidden" value="buy">
              <!--<div class="weui-count">-->
                <!--<a class="weui-count__btn weui-count__decrease"></a>-->
                <!--<input class="weui-count__number" type="number" value="1">-->
                <!--<a class="weui-count__btn weui-count__increase"></a>-->
              <!--</div>-->
            </div>
          </div>
        </div>
      </form>
      <div style="height:80px;"></div>
      <div class="weui-btn-area">
        <button id="confirmAdd" href="javascript:;" class="weui-btn weui-btn_primary">确定</button>
      </div>
    </div>
  </div>
</div>
<% block('footer').append(`<script src='/app/js/swiper.js'></script>`)%>
<% block('footer').append(`<script src="/js/spinner.js?v=0811" ></script>`); %>
<% block('footer').append(`<script>
jQuery(function(){
	var isLogin =${user?true:false}
    $(".swiper-container").swiper({})
    var $spinner = $(".spinner")
    $spinner.Spinner({
      callback:function(num) {
       console.log(num)
      }
    });
	var isSubmit = ''
    $('#addCart').on('click',function(){
    	if(!isLogin) return $.toast('您还没有登录','text')
    	isSubmit = 'cart'
    	$("#numPopup").popup()
    })
    $('#quickBuy').on('click', function () {
    	if(!isLogin) return $.toast('您还没有登录','text')
    	isSubmit = 'buy'
    	$("#numPopup").popup()
    })
    $('#confirmAdd').on('click', function () {
    	if(isSubmit ==='buy') {
            $('#form').submit()
    	}
    	// 添加到购物车
    	if(isSubmit === 'cart') {
            $.post('/add2Cart', $('#form').serialize()).done(function(res){
              console.log(res)
              if (res.code === 0) {
                var num = res.data.cart.reduce(function(before,item){
                  return before + item.goodsNum
                }, 0)
                $('#cartBadge').triggerHandler('change.cartBadge',[num])
                $.toast("添加成功", "text");
                $.closePopup()
              } else {
                $.toast(res.message || '添加错误','text')
              }
            })
    	}
    })
    // $('#chooseNum').find('.weui-popup__container').popup()
})
</script>`)%>